﻿using System;
using Castle.ActiveRecord.Queries;

namespace Pampa.MVC.ActiveRecord
{
    public class PampaScalarQuery<T> : ScalarQuery<T>
    {
        private readonly bool _cacheable = false;
        private readonly string _cacheRegion = null;

        public PampaScalarQuery(Type targetType, string query, bool cacheable = false, string cacheRegion = null, params object[] positionalParameters)
            : base(targetType, query, positionalParameters)
        {
            _cacheable = cacheable;
            _cacheRegion = cacheRegion;
        }

        public PampaScalarQuery(Type targetType, string query, bool cacheable = false, string cacheRegion = null)
            : base(targetType, query)
        {
            _cacheable = cacheable;
            _cacheRegion = cacheRegion;
        }

        public PampaScalarQuery(Type targetType, QueryLanguage queryLanguage, string query, bool cacheable = false, string cacheRegion = null, params object[] positionalParameters)
            : base(targetType, queryLanguage, query, positionalParameters)
        {
            _cacheable = cacheable;
            _cacheRegion = cacheRegion;
        }

        public PampaScalarQuery(Type targetType, QueryLanguage queryLanguage, string query, bool cacheable = false, string cacheRegion = null)
            : base(targetType, queryLanguage, query)
        {
            _cacheable = cacheable;
            _cacheRegion = cacheRegion;
        }

        protected override NHibernate.IQuery CreateQuery(NHibernate.ISession session)
        {
            var nhibQuery = base.CreateQuery(session);
            nhibQuery.SetCacheable(_cacheable);

            if (_cacheRegion != null)
                nhibQuery.SetCacheRegion(_cacheRegion);

            return nhibQuery;
        }
    }
}
